package com.yy.personalcv.controller;

import com.yy.personalcv.pojo.Education;
import com.yy.personalcv.pojo.UserInformation;
import com.yy.personalcv.responce.EducationResponce;
import com.yy.personalcv.service.EducationService;
import jakarta.annotation.Resource;
import com.yy.personalcv.tool.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class EducationController {
    @Resource
    private EducationService educationService;

    @RequestMapping(value = "/educationList.do")
    public String getEducationList(HttpServletRequest request, Model model,
                                   @RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "5") int size){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        int uid = userInformation.getId();
        int counts = getEducationCounts(uid);
        List<Education> educations = selectTen(page, size, uid);
        model.addAttribute("educations", educations);
        model.addAttribute("page", page);
        model.addAttribute("size", educations.size());
        model.addAttribute("max_size", size);
        model.addAttribute("counts", counts);
        model.addAttribute("userInformation", userInformation);
        return "education_page";
    }

    @RequestMapping(value = "/educationDetail.do")
    public String getEducationDetail(HttpServletRequest request, Model model,
                                     @RequestParam int id){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        int uid = userInformation.getId();
        Education education = getEducationDetail(id, uid);
        if(education == null){
            return "redirect:/educationList";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        EducationResponce educationResponce = new EducationResponce();
        educationResponce.setId(education.getId());
        educationResponce.setSchool(education.getSchool());
        educationResponce.setMajor(education.getMajor());
        educationResponce.setDegree(education.getDegree());
        educationResponce.setStartTime(sdf.format(education.getStart()));
        educationResponce.setEndTime(sdf.format(education.getEnd()));
        educationResponce.setModified(sdf.format(education.getModified()));
        educationResponce.setDisplay(education.getDisplay());
        educationResponce.setUid(education.getUid());
        model.addAttribute("education", educationResponce);
        model.addAttribute("userInformation", userInformation);
        return "education_detail_page";
    }

    @RequestMapping(value = "/addEducation.do")
    public String addEducation(HttpServletRequest request, Model model,
                               @RequestParam String school,
                               @RequestParam String major,
                               @RequestParam String degree,
                               @RequestParam String startTime,
                               @RequestParam String endTime){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(school) || StringUtils.getInstance().isNullOrEmpty(major) ||
                StringUtils.getInstance().isNullOrEmpty(degree) || StringUtils.getInstance().isNullOrEmpty(startTime) ||
                StringUtils.getInstance().isNullOrEmpty(endTime)){
            log.error("one of the parameters is null or empty");
            return "redirect:/educationList.do";
        }
        int uid = userInformation.getId();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Education education = new Education();
        education.setSchool(school);
        education.setMajor(major);
        education.setDegree(degree);
        try {
            education.setStart(sdf.parse(startTime));
            education.setEnd(sdf.parse(endTime));
        }catch (Exception e){
            log.error("parse date error");
            return "redirect:/educationList.do";
        }
        education.setUid(uid);
        educationService.addEducation(education);
        model.addAttribute("userInformation", userInformation);
        return "redirect:/educationList.do";
    }

    @RequestMapping(value = "/educationDelete.do")
    public String deleteEducation(HttpServletRequest request, Model model,
                              @RequestParam int id){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(id)){
            log.error("id is null");
            return "redirect:/educationList.do";
        }
        int uid = userInformation.getId();
        educationService.deleteEducation(id, uid);
        model.addAttribute("userInformation", userInformation);
        return "redirect:/educationList.do";
    }

    @RequestMapping(value = "/educationUpdate.do")
    public String updateEducation(HttpServletRequest request, Model model,
                              @RequestParam int id,
                              @RequestParam String school,
                              @RequestParam String major,
                              @RequestParam String degree,
                              @RequestParam String startTime,
                              @RequestParam String endTime){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(school) || StringUtils.getInstance().isNullOrEmpty(major) ||
                StringUtils.getInstance().isNullOrEmpty(degree) || StringUtils.getInstance().isNullOrEmpty(startTime) ||
                StringUtils.getInstance().isNullOrEmpty(endTime)){
            log.error("one of the parameters is null or empty");
            return "redirect:/educationList.do";
        }
        int uid = userInformation.getId();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Education education = new Education();
        education.setId(id);
        education.setSchool(school);
        education.setMajor(major);
        education.setDegree(degree);
        try {
            education.setStart(sdf.parse(startTime));
            education.setEnd(sdf.parse(endTime));
        }catch (Exception e){
            log.error("parse date error");
            return "redirect:/educationList.do";
        }
        education.setUid(uid);
        educationService.updateEducation(education);
        Education education1 = educationService.getEducationDetail(id, uid);
        EducationResponce educationResponce = new EducationResponce();
        educationResponce.setId(education1.getId());
        educationResponce.setSchool(education1.getSchool());
        educationResponce.setMajor(education1.getMajor());
        educationResponce.setDegree(education1.getDegree());
        educationResponce.setStartTime(sdf.format(education1.getStart()));
        educationResponce.setEndTime(sdf.format(education1.getEnd()));
        educationResponce.setModified(sdf.format(education1.getModified()));
        educationResponce.setDisplay(education1.getDisplay());
        educationResponce.setUid(education1.getUid());
        model.addAttribute("education", educationResponce);
        model.addAttribute("userInformation", userInformation);
        return "education_detail_page";
    }

    private int getEducationCounts(int uid){
        return educationService.getEducationCounts(uid);
    }

    private List<Education> selectTen(int page, int size, int uid){
        Map map = new HashMap<>();
        map.put("start", (page - 1) * size);
        map.put("size", size);
        map.put("uid", uid);
        return educationService.selectTen(map);
    }

    private  Education getEducationDetail(int id, int uid){
        return educationService.getEducationDetail(id, uid);
    }
}
